# -*- coding: utf-8 -*-
import sys, os
try:
        import pygtk
        pygtk.require("2.0")
except:
        pass
try:
        import gtk
        import gtk.glade
        import gobject
except:
        sys.exit(1)
from simpledcpp.core.base import DCPPPart, DCPPEvent
import urllib2

class MagnetDialog(DCPPPart):
	def __init__(self, parent):
		DCPPPart.__init__(self, parent)
		self._dialog = self.parent.wtree.get_widget("MagnetDialog")
		self.Events.Subscribe("OPEN_MAGNET", self._openmagnet)
		self.MagnetEntry = self.parent.wtree.get_widget("MagnetEntry")
		self.LabelFile = self.parent.wtree.get_widget("labelfile")
		self.LabelTTH = self.parent.wtree.get_widget("labeltth")
		self.LabelSize = self.parent.wtree.get_widget("labelsize")
		self.parent._fastConnect("MagnetCancel","clicked", self.hide)
		self.parent._fastConnect("MagnetOk","clicked", self._ok)
		self.parent._fastConnect("MagnetEntry",'changed', self._changed)
	
	def _openmagnet(self, Event):
                buf = Event.userdata[0]
                NAME, SIZE, TTH = self.parse_magnet(buf)
		self.MagnetEntry.set_text(buf)
                if NAME != None:
                        self.LabelFile.set_text(NAME)
                        self.LabelSize.set_text(SIZE)
                        self.LabelTTH.set_text(TTH)
                else:
#                       self.MagnetEntry.set_text("")
                        self.LabelFile.set_text(_("Unaviable"))
                        self.LabelSize.set_text(_("Unaviable"))
                        self.LabelTTH.set_text(_("Unaviable"))
                self._dialog.show()

	def _ok(self, *args, **kwargs):
		NAME, SIZE, TTH = self.parse_magnet(self.MagnetEntry.get_text())
		if NAME != None:
			self.Event(DCPPEvent("NEED_TASK", (TTH, NAME, SIZE, None)))
#			print NAME,SIZE,TTH
			self.hide()
		else:
			pass

	def _changed(self, *args, **kwargs):
                buf = self.MagnetEntry.get_text()
                NAME, SIZE, TTH = self.parse_magnet(buf)
                if NAME != None:
                        self.LabelFile.set_text(NAME)
                        self.LabelSize.set_text(SIZE)
                        self.LabelTTH.set_text(TTH)
                else:
                        self.LabelFile.set_text(_("Unaviable"))
                        self.LabelSize.set_text(_("Unaviable"))
                        self.LabelTTH.set_text(_("Unaviable"))


	def parse_magnet(self, magnet):
		try:
	                text = magnet
        	        if text.startswith('magnet:?'):
                	       	xl = None
                        	dn = None
	                        xt = None
        	                string = text.split('?')[1]
                	        for i in string.split('&'):
                      			if i.startswith('xt='):
                                	      	xt = i.split('=')[1]
	                                elif i.startswith('xl='):
        	                               	xl = i.split('=')[1]
                	                elif i.startswith('dn='):
                        	                dn = i.split('=')[1]
                                	if xt != None and xl != None and dn != None:
	                                       	dn = urllib2.unquote(dn)
        	                                TTH = xt.split(':')[-1]
						return (dn, xl, TTH)
#               	                        print "Request %s %s %s"%(TTH, dn, xl)
		except:
			pass
		return (None, None, None)

	def show(self, *args, **kwargs):
		buf = gtk.Clipboard().wait_for_text()
		NAME, SIZE, TTH = self.parse_magnet(buf)
		if NAME != None:
			self.MagnetEntry.set_text(buf)
			self.LabelFile.set_text(NAME)
			self.LabelSize.set_text(SIZE)
			self.LabelTTH.set_text(TTH)
		else:
			self.MagnetEntry.set_text("")
                        self.LabelFile.set_text(_("Unaviable"))
                        self.LabelSize.set_text(_("Unaviable"))
                        self.LabelTTH.set_text(_("Unaviable"))
		self._dialog.show()

	def hide(self, *args, **kwargs):
		self._dialog.hide()
